package com.alibaba.citrus.cr.common.enums.ordercenter;

import com.google.common.collect.Lists;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Getter
public enum SalesOrderTypeEnum {
    STD_ORDER("ZOR2", "销售公司标准订单"),
    FREE_ORDER("ZFD2", "销售公司免费订单"),
    REPLENISH_ORDER("ZFD3", "销售公司补货订单"), // 不支持的订单类型
    CASH_ORDER("ZBV", "销售公司现金销售订单"),
    E_COMMERCE_STD_ORDER("ZORO", "电商标准销售订单"),
    E_COMMERCE_AUTO_ORDER("ZORA", "电商自动销售订单"),

    // 一盘货新增枚举
    GROUP_FREE_ORDER("ZFOC", "免费销售订单"),
    GROUP_STD_ORDER("ZOR", "标准订单"),
    GROUP_DELIVERY_ORDER("ZOR8", "直发订单"),
    ;

    private final String code;
    private final String desc;

    SalesOrderTypeEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public static SalesOrderTypeEnum of(String code) {
        return Arrays.stream(SalesOrderTypeEnum.values()).filter(source -> Objects.equals(code, source.getCode())).findFirst().orElse(null);
    }

    public static String descByCode(String code) {
        SalesOrderTypeEnum typeEnum = of(code);
        return Objects.nonNull(typeEnum) ? typeEnum.getDesc() : null;
    }

    public static List<String> codes() {
        return Arrays.stream(SalesOrderTypeEnum.values()).map(SalesOrderTypeEnum::getCode).collect(Collectors.toList());
    }

    public static List<String> companyOrderCodes() {
        return Lists.newArrayList(STD_ORDER.getCode(), FREE_ORDER.getCode(), CASH_ORDER.getCode(), E_COMMERCE_STD_ORDER.getCode(), E_COMMERCE_AUTO_ORDER.getCode(), REPLENISH_ORDER.getCode());
    }

    public static List<String> groupOrderCodes() {
        return Lists.newArrayList(GROUP_FREE_ORDER.getCode(), GROUP_STD_ORDER.getCode(), GROUP_DELIVERY_ORDER.getCode());
    }

    public static SalesOrderTypeEnum groupOrderOf(String code) {
        List<String> groupOrderTypes = Lists.newArrayList(
                GROUP_FREE_ORDER.getCode(),
                GROUP_STD_ORDER.getCode(),
                GROUP_DELIVERY_ORDER.getCode()
        );
        if (groupOrderTypes.contains(code)) {
            return of(code);
        }
        return null;
    }
}
